﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Sharpie.Libs;
using Microsoft.Xna.Framework.Input.Touch;
#if ANDROID
using SharpieAndroid;
using SharpieAndroid.States;
using IndependentResolutionRendering;
#endif

namespace Sharpie
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Main : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;
        public static TouchPanelCapabilities TouchCapabilities = TouchPanel.GetCapabilities();
        public static readonly string Mapspath = Path.GetFullPath(Path.Combine(Activity1.AppDataPath, "Maps"));
        public string WindowTitle = "Sharpie";

        public Main()
        {
			graphics = new GraphicsDeviceManager(this);
			graphics.IsFullScreen = true;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            if (!Directory.Exists(Mapspath))
                Directory.CreateDirectory(Mapspath);
            this.TargetElapsedTime = TimeSpan.FromSeconds(1f / 60f);
            base.Initialize();
            LocaleInit();
            TouchPanel.EnabledGestures = GestureType.Tap;
            StateManager.AddScreen("mainmenu", new Screen(new MainMenu(this)));
			StateManager.SetMainState("mainmenu");
            StateManager.AddScreen("settmenu", new Screen(new SettingsMenu(this)));
			StateManager.AddScreen("mapmenu", new Screen(new MapSelect(this)));
        }

        private void LocaleInit()
        {
            bool CopyDefault = false;
			string defaultLangPath = Path.Combine(Locale.LangDir, Locale.defaultLang);
            if (!File.Exists(defaultLangPath))
            {
                CopyDefault = true;
            }
            else
            {
                if (File.ReadAllText(defaultLangPath) != new StreamReader(Activity.Assets.Open(@"Lang/en.xml")).ReadToEnd())
                {
                    CopyDefault = true;
                }
            }

            if (CopyDefault)
            {
                using (StreamWriter sw = File.CreateText(defaultLangPath))
                {
                    using (StreamReader sr = new StreamReader(Activity.Assets.Open(@"Lang/en.xml")))
                    {
                        while (!sr.EndOfStream)
                        {
                            sw.WriteLine(sr.ReadLine());
                        }
                    }
                    sw.Flush();
                }
            }

            if (!Locale.LoadLang(Config.Nodes["Language"]))
            {
                if (Config.Nodes["Language"] == "")
                {
                    // lang choose Android UI
                }
                Config.Nodes["Language"] = Locale.defaultLang;
                string saveerror;
                Config.Save(out saveerror);
                if (saveerror != "")
                {
                    // TODO: error Msg
                }
                Locale.LoadLang(Config.Nodes["Language"]);
            }
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
			StateManager.SetBackground(GraphicsDevice, Content.Load<Texture2D>("Images\\leaves"));
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (IsActive)
            {
                StateManager.Update(gameTime);
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        ///
        protected override void Draw(GameTime gameTime)
        {
            StateManager.Draw(gameTime);
            base.Draw(gameTime);
        }
    }
}